package com.gzz.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.gzz.mapper.TicketMapper;
import com.gzz.mapper.TikcetOrderDetailMapper;
import com.gzz.pojo.Ticket;
import com.gzz.pojo.TicketOrder;
import com.gzz.mapper.TicketOrderMapper;
import com.gzz.pojo.TikcetOrderDetail;
import com.gzz.service.TicketOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 周佳成
 * @since 2023-11-20
 */
@Service
public class TicketOrderServiceImpl extends ServiceImpl<TicketOrderMapper, TicketOrder> implements TicketOrderService {

	@Resource
	TicketMapper ticketMapper;
	@Resource
	TikcetOrderDetailMapper tikcetOrderDetailMapper;

	@Override
	@Transactional(rollbackFor = RuntimeException.class)
	public void delOrderTimeoutById(Long id) {
		TicketOrder ticketOrder = getById(id);
		Long TicketId = ticketOrder.getTicketId();
		Integer num = ticketOrder.getNum();


		Ticket ticket = ticketMapper.selectById(TicketId);

		ticket.setNum(ticket.getNum()+num);

		int r = ticketMapper.updateById(ticket);

		if(r<=0){
			throw new RuntimeException("更新票失败在删除超时订单相关信息时");
		}
		LambdaUpdateWrapper<TikcetOrderDetail> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
		lambdaUpdateWrapper.eq(TikcetOrderDetail::getOrderId,id);

		int dr = tikcetOrderDetailMapper.delete(lambdaUpdateWrapper);
		if(dr<=0){
			throw new RuntimeException("更订单明细表失败在删除超时订单相关信息时");
		}

		boolean od = removeById(id);

		if(!od){
			throw new RuntimeException("删除超时订单失败");
		}


	}
}
